'''
Author: ZhangRui
Date: 2023-06-06 10:37:18
LastEditors: ZhangRui
LastEditTime: 2023-06-06 17:36:37
FilePath: \get-goods-info\jd_address.py
Description:
'''
import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options


class JD:
    driver: WebDriver = None

    def __init__(self):
        self.init_driver()

    def init_driver(self):

        chrome_options = Options()
        chrome_options.debugger_address = '127.0.0.1:9223'
        self.driver = webdriver.Chrome(options=chrome_options)

        # option = webdriver.ChromeOptions()
        # option.add_experimental_option("detach", True)
        # # option.add_argument("--disable-blink-features")
        # # 隐藏selenium的自动自动控制的功能的显示防止被检测
        # option.add_argument("--disable-blink-features=AutomationControlled")
        # # 设置修改selenium的特征值，防止被检测到
        # option.add_experimental_option(
        #       'excludeSwitches', ['enable-automation'])

        # self.driver = webdriver.Chrome(
        #       "C:\Program Files\Google\Chrome\Application\chromedriver.exe", options=option)
        # self.driver.maximize_window()  # 设置浏览器打开全屏
        print("初始化浏览器完成~")

    def start(self):

        print(self.driver)

        # # 点击 添加收货地址
        # button = self.driver.find_element(By.ID, 'edit-add-dialog')
        # button.click()

        # # 等待1秒钟
        # self.driver.implicitly_wait(2)

        # # 鼠标移动到 所在地区
        # select = self.driver.find_element(By.ID, "jd_area")

        # ActionChains(self.driver).move_to_element(select).perform()

        # # 等待1秒钟
        # self.driver.implicitly_wait(2)

        # 找到hover之后显示的元素
        # hover_element = self.driver.find_element(By.CLASS_NAME, "ui-area-hover")

        # # 验证是否显示
        # key = hover_element.is_displayed()
        # print("key: %s", key)
        # assert key
        time.sleep(3)

        element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, "jd_area")))

        jd_area = self.driver.find_element(By.ID, "jd_area")

        areaContent = jd_area.find_element(
            By.CLASS_NAME, "ui-area-content")

        省区域 = areaContent.find_elements(
            By.CLASS_NAME, "ui-switchable-panel")[0]

        省份列表 = 省区域.find_element(
            By.CLASS_NAME, "ui-area-content-list").find_elements(By.TAG_NAME, "li")

        # 打开CSV文件并准备写入数据
        with open('output.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)

            # 写入标题行
            writer.writerow(['parentCode', 'code', '名称'])

            for i in range(len(省份列表)):
                if i > 9:
                    continue

                wait = WebDriverWait(self.driver, 10)
                # 等待页面加载
                wait.until(EC.presence_of_element_located(
                    (By.CLASS_NAME, 'ui-area-hover')))
                
                self.driver.find_element(
                    By.CLASS_NAME, "ui-area-tab").find_elements(By.CLASS_NAME, "ui-switchable-item")[0].click()
                # 省份列表[i].click()
                self.driver.execute_script("arguments[0].click();", 省份列表[i])

                print(省份列表[i].find_element(
                      By.TAG_NAME, "a").get_attribute("data-id"))
                print(省份列表[i].find_element(By.TAG_NAME,
                      "a").get_attribute("innerText"))

                writer.writerow(["0", 省份列表[i].find_element(
                    By.TAG_NAME, "a").get_attribute("data-id"), 省份列表[i].find_element(By.TAG_NAME,
                                                                                     "a").get_attribute("innerText")])

                time.sleep(1)


                市区域 = areaContent.find_elements(
                    By.CLASS_NAME, "ui-switchable-panel")[1]

                市列表 = 市区域.find_element(
                    By.CLASS_NAME, "ui-area-content-list").find_elements(By.TAG_NAME, "li")

                for j in range(len(市列表)):

                    self.driver.find_element(
                        By.CLASS_NAME, "ui-area-tab").find_elements(By.CLASS_NAME, "ui-switchable-item")[1].click()
                    # 市列表[i].click()
                    self.driver.execute_script("arguments[0].click();", 市列表[j])

                    # print(市列表[j].find_element(
                    #       By.TAG_NAME, "a").get_attribute("data-id"))
                    # print(市列表[j].find_element(By.TAG_NAME,
                    #       "a").get_attribute("innerText"))

                    writer.writerow([省份列表[i].find_element(
                        By.TAG_NAME, "a").get_attribute("data-id"), 市列表[j].find_element(
                        By.TAG_NAME, "a").get_attribute("data-id"), 市列表[j].find_element(By.TAG_NAME,
                                                                                        "a").get_attribute("innerText")])
                    time.sleep(1)

                    区域 = areaContent.find_elements(
                        By.CLASS_NAME, "ui-switchable-panel")[2]

                    区列表 = 区域.find_element(
                        By.CLASS_NAME, "ui-area-content-list").find_elements(By.TAG_NAME, "li")

                    for 区 in 区列表:
                        # print(区.find_element(
                        #       By.TAG_NAME, "a").get_attribute("data-id"))
                        # print(区.find_element(By.TAG_NAME,
                        #       "a").get_attribute("innerText"))

                        writer.writerow([市列表[j].find_element(
                            By.TAG_NAME, "a").get_attribute("data-id"), 区.find_element(
                            By.TAG_NAME, "a").get_attribute("data-id"), 区.find_element(By.TAG_NAME,
                                                                                       "a").get_attribute("innerText")])

                    # if j == len(市列表):
                        
                    #     self.driver.execute_script(
                    #         "arguments[0].click();", 省份列表[i])

                    # self.export()

    def export(self):
        with open('output.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile, delimiter=',')
            for line in self.data:
                writer.writerow([line])


def main():
    JD().start()


if __name__ == "__main__":
    main()
